package com.harvest.scientific.ladder.base

import android.content.Context
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.harvest.scientific.ladder.dialog.LoadingDialog
import com.harvest.scientific.ladder.page.AppModel
import com.harvest.scientific.ladder.util.FlowBus
import com.harvest.scientific.ladder.util.FlowBusConstant
import com.harvest.scientific.ladder.util.LocaleHelper

open class BaActivity : AppCompatActivity(), ILoading {

    protected val appModel by viewModels<AppModel>()
    protected var isFinished = false
    protected var loadingDialog: LoadingDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        LocaleHelper.setLocale(this, LocaleHelper.getPersistedData(this, "fr"))
        super.onCreate(savedInstanceState)

        FlowBus.with<Unit>(FlowBusConstant.UPDATE_LOCALE).register(this) {
            recreate()
        }
    }

    fun switchLanguage(language: String) {
        LocaleHelper.setLocale(this, language)
        recreate()
    }

    override fun attachBaseContext(newBase: Context?) {
        newBase ?: return super.attachBaseContext(newBase)
        val locale = LocaleHelper.getPersistedData(newBase, "fr")
        super.attachBaseContext(LocaleHelper.updateResources(newBase, locale))
    }


    override fun showLoading() {
        loadingDialog = LoadingDialog.Builder(this)
            .setIconType(LoadingDialog.Builder.ICON_TYPE_LOADING)
            .create(true)
        loadingDialog?.show()
    }

    override fun dismissLoading() {
        loadingDialog?.dismiss()
    }

    override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
        handleFinish()
        return super.onOptionsItemSelected(menuItem)
    }

    open fun handleFinish() {
        supportFinishAfterTransition()
    }

    override fun onPause() {
        super.onPause()
        if (!isFinished && isFinishing) {
            isFinished = true
            onRecycler()
        }
    }

    override fun onDestroy() {
        if (!isFinished) {
            isFinished = true
            onRecycler()
        }
        super.onDestroy()
    }

    open fun onRecycler() {

    }

    protected fun addFragment(fragment: BaseFragment<*>, tag: String? = ""): BaseFragment<*>? {
        return addFragment(fragment, false, tag)
    }

    protected fun addFragment(fragment: BaseFragment<*>, needAddToBackStack: Boolean, tag: String? = ""): BaseFragment<*>? {
        val fm = supportFragmentManager
        val fragmentTransaction = fm.beginTransaction()
        if(tag.isNullOrEmpty()) {
            fragmentTransaction.replace(fragment.getContainerId(), fragment)
        } else {
            fragmentTransaction.replace(fragment.getContainerId(), fragment, tag)
        }
        if (needAddToBackStack) {
            fragmentTransaction.addToBackStack(null)
        }
        try {
            fragmentTransaction.commitAllowingStateLoss()
        } catch (e: Exception) {
        }
        return fragment
    }

}